<template>
  <div>
    <!--主体-->
    <el-table :data="tableData" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              stripe>
      <el-table-column prop="cno" label="课程号"></el-table-column>
      <el-table-column prop="cname" label="课程名"></el-table-column>
      <el-table-column prop="cpno" label="先行课号"></el-table-column>
      <el-table-column prop="cpname" label="先行课"></el-table-column>
      <el-table-column prop="ccredit" label="学分"></el-table-column>
      <el-table-column prop="tno" label="教工号"></el-table-column>
      <el-table-column prop="tname" label="教师"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <!--scope.row就是当前行数据-->
          <el-button type="primary" :disabled="status === '0'" @click="select(scope.row.cno,scope.row.tno)">选课
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request3 from "@/utils/request3";

export default {
  name: "SelectCourse",
  data() {
    return {
      name: JSON.parse(Cookies.get('student')).username,
      total: 0,
      status: '',
      count: '',
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request3.get('/selectCourse/page', {params: this.params}).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
      request3.get('/timeSet/getStudent').then(res => {
        this.status = res.data.status
        this.count = res.data.count
      })
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },
    select(cno, tno) {
      request3.get('/selectCourse/recordPage/' + this.name).then(res => {
        if (parseInt(this.count) < res.data.list.length+1) {
          this.$notify.warning('选课数量已够')
        } else {
          request3.get('/selectCourse/querySct/' + this.name + '/' + cno + '/' + tno).then(res => {
            if (res.code === '-1') {
              this.$notify.error('已选过该课程，请不要再次选该门课程')
            } else {
              request3.get('/selectCourse/addSct/' + this.name + '/' + cno + '/' + tno).then(res => {
                if (res.code === '200') {
                  this.$notify.success('选课成功')
                } else {
                  this.$notify.success('选课失败')
                }
              })
            }
          })
        }
      })
    }
  }
}
</script>
